<template>
	<view style="width: 100%;height: 100%;">
		<!-- 头部 -->
		<view class="theme-container" style="position: relative;z-index: -1;">
			<view :style="{height: sConf.tH+'px'}"></view>
			<view class="top-font-title"
				style="font-size: x-large;font-weight: bolder;padding: 35px 0;color: #fff;text-align: center;">
				{{title}}
			</view>
		</view>
		<!--垂直滚动-->
		<scroll-view class="mainContent" scroll-y style="width: 100%;height: 80vh;padding: 5px 10px;">
			<TnListItem v-for="(staff, index) in childData" :key="index">
				<view class="midnight" style="padding: 5px;border-radius: 5px;margin-bottom: 5px;">
					<sub-staff-card :data="staff.staff" :note="staff.note" :isDesc="false" isTarget
						@navChange="navChange" @levelEvent="levelEvent" @masterEvent="masterEvent"
						@noteEvent="noteEvent"></sub-staff-card>
				</view>
			</TnListItem>
			<view v-if="childData.length === 0" style="margin-top: 20px;">
				<tn-empty mode="list"></tn-empty>
			</view>
			<view style="height: 100px;"></view>
		</scroll-view>
		<!-- 顶部自定义导航 -->
		<tn-navbar :placeholder="true" fixed bg-color="#FFFFFF00" text-color="#fff">{{sConf.title}} •
			{{title}}</tn-navbar>
	</view>

	<!-- 模态窗口 -->
	<tn-action-sheet ref="actionSheetRing">
		<template #cancel>
			<view class="custom-cancel" style="margin-bottom: 60px;">取消</view>
		</template>
	</tn-action-sheet>
</template>

<script>
	import utils from '@/common/utils';
	import psMethod from '@/common/psMethod';
	export default {
		data() {
			return {
				topH: 0,
				title: '团队成员',
				sConf: getApp().globalData,
				childData: [], // 成员列表
				dataSelf: null, // 个人信息
				dataParent: null, // 上级
			}
		},
		onLoad() {
			console.log('onLoad');
			this.loadDefData()
		},
		methods: {
			loadDefData() {
				this.loadUserinfoRead(this.sConf.auth.id)
				this.loadGroupChildList(this.sConf.auth.id)
			},
			/**
			 * 加载个人信息
			 */
			loadUserinfoRead(e) {
				let host = this.sConf.host + '/weapps/userinfo/' + e + '/';
				let token = this.sConf.token || '';
				utils.authenticatorOnRead(host, token).then((res) => {
					// console.log('加载个人信息', res);
					if (e === this.sConf.auth.id) {
						this.dataSelf = res;
					} else {
						this.dataParent = res;
					}
				})
			},
			/**
			 * 加载成员列表			
			 */
			loadGroupChildList(e) {
				let host = this.sConf.host + '/weapps/group/';
				let token = this.sConf.token || ''
				let data = {
					parent: e
				}
				utils.authenticatorOnList(host, token, data).then((res) => {
					console.log('团队成员', res);
					this.childData = res
				})
			},
			/**
			 * 导航事件
			 */
			navChange(e) {
				console.log('navChange', e);
				let args = encodeURIComponent('parent=' + e)
				uni.navigateTo({
					url: '/pageb/staff/staff?scene=' + args
				})
			},
			/**
			 * 权益事件
			 */
			levelEvent(e) {
				console.log('levelEvent', e);
				let host = this.sConf.host + '/weapps/levelCard/';
				let token = this.sConf.token || '';
				let data = {
					status: 1,
					sid: this.sConf.auth.id
				}
				utils.authenticatorOnList(host, token, data).then(res => {
					// console.log('加载权益券', res);
					let tmpData = []
					res.forEach(item => {
						if (item.cardqy.level === 1 || item.cardqy.level > e.level) {
							tmpData.push({
								value: item.id,
								text: item.cardqy.name,
								desc: '权益券'
							});
						}
					});
					this.levelPutEvent(e.sid, e.name, tmpData);
				}).catch(err => {
					console.log('加载权益券__失败', err);
				})
			},
			levelPutEvent(e, n, data) {
				let that = this;
				this.$refs.actionSheetRing.show({
					actions: data,
					title: '请选择权益券',
					cancel: () => {
						return true
					},
					select: (index, value) => {
						// console.log('选项被点击', index, value)
						uni.showModal({
							title: '确认',
							content: '是否要消耗一张 ' + data[index].text + ' 权益卡给-> ' + n + ' ?',
							success(res) {
								if (res.confirm) {
									console.log('确认');
									that.levelPostEvent(value, e)
								}
							}
						})
						return true
					}
				})
			},
			/**
			 * 核销权益券
			 * @param {Number} e 卡券ID
			 * @param {Number} s 目标ID
			 */
			levelPostEvent(e, s) {
				let host = this.sConf.host + '/weapps/putLevel/';
				let token = this.sConf.token || '';
				let data = {
					cardid: e,
					target: s
				}
				utils.authenticatorOnPost(host, token, data).then(res => {
					// console.log('提升权益', res);
					this.loadDefData()
					uni.showToast({
						title: '完成'
					})
				}).catch(err => {
					console.log('提升权益__失败', err);
					uni.showToast({
						icon: 'error',
						title: '失败'
					})
				})
			},
			/**
			 * 红包事件
			 */
			masterEvent(e) {
				// console.log('masterEvent', e);
				let that = this
				uni.showModal({
					title: '红包抵扣额度 当前余额:' + (that.dataSelf.master.amount / 100).toFixed(2),
					editable: true,
					placeholderText: '派发红包抵扣额度 -> ' + e.name,
					success(res) {
						if (res.confirm) {
							// console.log(res.content);
							if (!utils.isPlaceMoney(res.content)) {
								uni.showToast({
									icon: 'error',
									title: '校验金额失败'
								})
								return
							}
							if ((Number(res.content) * 100) > that.dataSelf.master.amount) {
								uni.showToast({
									icon: 'error',
									title: '当前额度不足'
								})
								return
							}
							that.masterPostEvent(Number(res.content) * 100, e.sid)
						}
					}
				})
			},
			/**
			 * 修改别名事件
			 */
			noteEvent(e) {
				console.log('noteEvent', e);
				let host = this.sConf.host + '/weapps/epithet/';
				let token = this.sConf.token || '';
				let data = {
					sid: this.sConf.auth.id,
					child: e.child,
					name: e.name
				}
				if (Boolean(e.id)) {
					host += e.id + '/'
					psMethod.authenticatorPatchSync(host, token, data).then(res => {
						console.log('修改别名', res);
						this.loadGroupChildList(this.sConf.auth.id)
					})
				} else {
					psMethod.authenticatorPostSync(host, token, data).then(res => {
						console.log('创建别名', res);
						this.loadGroupChildList(this.sConf.auth.id)
					})
				}


			},
			/**
			 * @param {Number} e 金额
			 * @param {Number} s 目标
			 */
			masterPostEvent(e, s) {
				let host = this.sConf.host + '/weapps/putMaster/';
				let token = this.sConf.token || '';
				let data = {
					money: e,
					target: s
				}
				utils.authenticatorOnPost(host, token, data).then(res => {
					// console.log('提升权益', res);
					this.loadDefData()
					uni.showToast({
						title: '完成'
					})
				}).catch(err => {
					console.log('提升权益__失败', err);
					uni.showToast({
						icon: 'error',
						title: '失败'
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>